//SPDX-FileCopyrightText: Copyright 2022-2024 深圳市同心圆网络有限公司
//SPDX-License-Identifier: GPL-3.0-only

package fs_dao

import (
	"context"

	"atomgit.com/openlinksaas/api-server/dao/db_ns"
	"go.mongodb.org/mongo-driver/bson"
	"go.mongodb.org/mongo-driver/mongo"
)

const _GLOBAL_FS_APP_STORE = "appStore"
const _GLOBAL_FS_WIDGET_STORE = "widgetStore"
const _GLOBAL_FS_SW_STORE = "swStore"
const _GLOBAL_FS_DOCKER_TEMPLATE = "dockerTemplate"

type GlobalFsDbItem struct {
	Key  string `bson:"_id"`
	FsId string `bson:"fsId"`
}

type _GlobalFsDao struct {
	mc *mongo.Collection
}

func newGlobalFsDao(db *mongo.Database) (*_GlobalFsDao, error) {
	mc := db.Collection(db_ns.DB_NS_FS + "global_fs")
	return &_GlobalFsDao{mc: mc}, nil

}

func (db *_GlobalFsDao) InsertAppStoreFs(ctx context.Context, fsId string) error {
	_, err := db.mc.InsertOne(ctx, &GlobalFsDbItem{
		Key:  _GLOBAL_FS_APP_STORE,
		FsId: fsId,
	})
	return err
}

func (db *_GlobalFsDao) ExistAppStoreFs(ctx context.Context) (bool, error) {
	count, err := db.mc.CountDocuments(ctx, bson.M{"_id": _GLOBAL_FS_APP_STORE})
	return count > 0, err
}

func (db *_GlobalFsDao) GetAppStoreFs(ctx context.Context) (string, error) {
	dbItem := &GlobalFsDbItem{}
	err := db.mc.FindOne(ctx, bson.M{"_id": _GLOBAL_FS_APP_STORE}).Decode(dbItem)
	if err != nil {
		return "", err
	}
	return dbItem.FsId, nil
}

func (db *_GlobalFsDao) InsertWidgetStoreFs(ctx context.Context, fsId string) error {
	_, err := db.mc.InsertOne(ctx, &GlobalFsDbItem{
		Key:  _GLOBAL_FS_WIDGET_STORE,
		FsId: fsId,
	})
	return err
}

func (db *_GlobalFsDao) ExistWidgetStoreFs(ctx context.Context) (bool, error) {
	count, err := db.mc.CountDocuments(ctx, bson.M{"_id": _GLOBAL_FS_WIDGET_STORE})
	return count > 0, err
}

func (db *_GlobalFsDao) GetWidgetStoreFs(ctx context.Context) (string, error) {
	dbItem := &GlobalFsDbItem{}
	err := db.mc.FindOne(ctx, bson.M{"_id": _GLOBAL_FS_WIDGET_STORE}).Decode(dbItem)
	if err != nil {
		return "", err
	}
	return dbItem.FsId, nil
}

func (db *_GlobalFsDao) InsertDockerTemplateFs(ctx context.Context, fsId string) error {
	_, err := db.mc.InsertOne(ctx, &GlobalFsDbItem{
		Key:  _GLOBAL_FS_DOCKER_TEMPLATE,
		FsId: fsId,
	})
	return err
}

func (db *_GlobalFsDao) ExistDockerTemplateFs(ctx context.Context) (bool, error) {
	count, err := db.mc.CountDocuments(ctx, bson.M{"_id": _GLOBAL_FS_DOCKER_TEMPLATE})
	return count > 0, err
}

func (db *_GlobalFsDao) GetDockerTemplateFs(ctx context.Context) (string, error) {
	dbItem := &GlobalFsDbItem{}
	err := db.mc.FindOne(ctx, bson.M{"_id": _GLOBAL_FS_DOCKER_TEMPLATE}).Decode(dbItem)
	if err != nil {
		return "", err
	}
	return dbItem.FsId, nil
}

func (db *_GlobalFsDao) InsertSwStoreFs(ctx context.Context, fsId string) error {
	_, err := db.mc.InsertOne(ctx, &GlobalFsDbItem{
		Key:  _GLOBAL_FS_SW_STORE,
		FsId: fsId,
	})
	return err
}

func (db *_GlobalFsDao) ExistSwStoreFs(ctx context.Context) (bool, error) {
	count, err := db.mc.CountDocuments(ctx, bson.M{"_id": _GLOBAL_FS_SW_STORE})
	return count > 0, err
}

func (db *_GlobalFsDao) GetSwStoreFs(ctx context.Context) (string, error) {
	dbItem := &GlobalFsDbItem{}
	err := db.mc.FindOne(ctx, bson.M{"_id": _GLOBAL_FS_SW_STORE}).Decode(dbItem)
	if err != nil {
		return "", err
	}
	return dbItem.FsId, nil
}
